//
//  XTComposeToolBar.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/20.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit

//btn的枚举类型
enum ComposeToolBarBtnType:Int{
    case Picture=0
    //@
    case Mention=1
    //话题
    case Trend = 2
    case Emoticon = 3
    case Add = 4
}

//UIStackView只是一个容器，不具备渲染效果，即无法单独进行显示
class XTComposeToolBar: UIStackView {

    var emoticonBtn : UIButton?
    
    //闭包
    var didSelectedComposeToolBarBtnClosure:((type:ComposeToolBarBtnType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.redColor()
        
        //排列方式
        axis = .Horizontal
        //布局方式
        distribution = .FillEqually
    
        addChildBtn("compose_toolbar_picture", type: ComposeToolBarBtnType.Picture)
        addChildBtn("compose_trendbutton_background", type: ComposeToolBarBtnType.Mention)
        addChildBtn("compose_mentionbutton_background", type: ComposeToolBarBtnType.Trend)
        emoticonBtn = addChildBtn("compose_emoticonbutton_background", type: ComposeToolBarBtnType.Emoticon)
        addChildBtn("compose_add_background", type: ComposeToolBarBtnType.Add)

    }
    
    
    
    //添加子控件btn的方法
    private func addChildBtn(imgName:String,type:ComposeToolBarBtnType)->UIButton{
    
        let btn = UIButton()
        btn.setImage(UIImage(named: imgName), forState: .Normal)
        btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        btn.tag = type.rawValue
        
        //去掉高亮效果的方法（如果不去掉，则点击时按钮的背景会出现系统模拟出的变暗的高亮效果）
        //adjusts 调整
        btn.adjustsImageWhenHighlighted = false
        
        //点击按钮调用的方法
        btn.addTarget(self, action: "btnClick:", forControlEvents: .TouchUpInside)
        
        
        addArrangedSubview(btn)
        
        return btn
    }
    
    //在其它方法中需要调用的一个方法，需要在前面加@objc才能呗找到
    @objc private func btnClick(btn:UIButton){
        //将btn的tag转换成枚举值
        let type = ComposeToolBarBtnType(rawValue: btn.tag)!
        //将枚举值传递出去
        didSelectedComposeToolBarBtnClosure?(type:type)
    
    }
    
    //切换表情图片的图标的方法
    func showEmoticonIcon(isEmoticon:Bool){
        var icon = "compose_emoticonbutton_background"
        if isEmoticon{
            icon = "compose_keyboardbutton_background"
        }
        emoticonBtn?.setImage(UIImage(named: icon), forState: .Normal)
        emoticonBtn?.setImage(UIImage(named: "\(icon)_highlighted"), forState: .Highlighted)
    
    }
}
